home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GDEVSUN.C < prev    next >
C/C++ Source or Header  |  1992-02-16  |  11KB  |  342 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gdevsun.c */
  21. /* SunView driver for Ghostscript */
  22. #include <suntool/sunview.h>
  23. #include <suntool/canvas.h>
  24. #include <stdio.h>
  25. /* Hack to get around the fact that something in the SunView library */
  26. /* defines uint and ushort.... */
  27. #  define uint _uint
  28. #  define ushort _ushort
  29. #include "gx.h"            /* for gx_bitmap; includes std.h */
  30. #  undef _uint
  31. #  undef _ushort
  32. #include "gsmatrix.h"            /* needed for gxdevice.h */
  33. #include "gxdevice.h"
  34.  
  35. #ifndef DEFAULT_DPI
  36. #  define DEFAULT_DPI 75        /* Sun standard monitor */
  37. #endif
  38.  
  39. #define COLOR 1                /* choose color or monochrome */
  40.  
  41. /* Procedures */
  42. dev_proc_open_device(sun_open);
  43. dev_proc_sync_output(sun_sync);
  44. dev_proc_close_device(sun_close);
  45. dev_proc_map_rgb_color(sun_map_rgb_color);
  46. dev_proc_map_color_rgb(sun_map_color_rgb);
  47. dev_proc_fill_rectangle(sun_fill_rectangle);
  48. dev_proc_copy_mono(sun_copy_mono);
  49. dev_proc_copy_color(sun_copy_color);
  50. dev_proc_draw_line(sun_draw_line);
  51.  
  52. /* The device descriptor */
  53. private gx_device_procs sun_procs = {
  54.     sun_open,
  55.     gx_default_get_initial_matrix,
  56.     sun_sync,
  57.     gx_default_output_page,
  58.     sun_close,
  59.     sun_map_rgb_color,
  60.     sun_map_color_rgb,
  61.     sun_fill_rectangle,
  62.     gx_default_tile_rectangle,
  63.     sun_copy_mono,
  64.     sun_copy_color,
  65.     sun_draw_line,
  66.     gx_default_get_bits,
  67.     gx_default_get_props,
  68.     gx_default_put_props
  69. };
  70.  
  71. /* Define the SunView device */
  72. typedef struct gx_device_sun {
  73.     gx_device_common;
  74.     Frame frame;
  75.     Canvas canvas;
  76.     Pixwin *pw;
  77.     struct mpr_data mpr;
  78.     Pixrect    pr;
  79. #if !arch_is_big_endian            /* need to swap bits & bytes */
  80. #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
  81.     byte swap_buf[BUF_WIDTH_BYTES];
  82. #endif
  83. } gx_device_sun;
  84.  
  85. #if !arch_is_big_endian
  86. /* Define a table for reversing bit order. */
  87. static byte reverse_bits[256] = {
  88.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  89.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  90.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  91.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  92.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  93.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  94.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  95.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  96.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  97.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  98.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  99.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  100.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  101.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  102.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  103.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  104. };
  105. #endif
  106.  
  107. /* The instance is public. */
  108. #if COLOR                /* ****** */
  109. gx_device_sun gs_sunview_device = {
  110.     sizeof(gx_device_sun),
  111.     &sun_procs,
  112.     "sunview",
  113.      (int)(8.5*DEFAULT_DPI), (int)(11*DEFAULT_DPI),    /* x and y extent */
  114.      DEFAULT_DPI, DEFAULT_DPI,    /* x and y density */
  115.         /* Following parameters are initialized for color */
  116.     1,            /* has color */
  117.     3,            /* max r-g-b value */
  118.     8,            /* bits per color pixel */
  119.      0,            /* connection not initialized */
  120.         /* End of monochrome/color parameters */
  121. };
  122. #else                    /* ****** */
  123. gx_device_sun gs_sunview_device = {
  124.     sizeof(gx_device_sun),
  125.     &sun_procs,
  126.     "sunview",
  127.      (int)(8.5*DEFAULT_DPI), (int)(11*DEFAULT_DPI),    /* x and y extent */
  128.      DEFAULT_DPI, DEFAULT_DPI,    /* x and y density */
  129.         /* Following parameters are initialized for monochrome */
  130.     0,            /* has color */
  131.     1,            /* max r-g-b value */
  132.     1,            /* bits per color pixel */
  133.      0,            /* connection not initialized */
  134.         /* End of monochrome/color parameters */
  135. };
  136. #endif                    /* ****** */
  137.  
  138. /* Macro for casting gx_device argument */
  139. #define xdev ((gx_device_sun *)dev)
  140.  
  141. /* Macro to validate arguments */
  142. #define check_rect()\
  143.     if ( w <= 0 || h <= 0 ) return 0;\
  144.     if ( x < 0 || x > xdev->width - w || y < 0 || y > xdev->height - h )\
  145.         return -1
  146.  
  147. /* Initialize the device. */
  148. private Notify_value destroy_func();
  149. int
  150. sun_open(register gx_device *dev)
  151. {
  152. #ifdef gs_DEBUG
  153. if ( gs_debug['X'] )
  154.     { extern int _Xdebug;
  155.       _Xdebug = 1;
  156.     }
  157. #endif
  158.     if (xdev->frame == (Frame)0)
  159.         xdev->frame =
  160.         window_create(NULL, FRAME, FRAME_LABEL, "ghostscript",
  161.             WIN_HEIGHT, xdev->width + 20,
  162.             WIN_HEIGHT, xdev->height + 40,    0);
  163.     xdev->canvas = window_create(xdev->frame, CANVAS,
  164.             CANVAS_AUTO_EXPAND,        FALSE,
  165.             CANVAS_AUTO_SHRINK,        FALSE,
  166.             CANVAS_WIDTH,            xdev->width,
  167.             CANVAS_HEIGHT,            xdev->height,
  168.             WIN_VERTICAL_SCROLLBAR,        scrollbar_create(0),
  169.             WIN_HORIZONTAL_SCROLLBAR,    scrollbar_create(0),
  170.         0);
  171.     xdev->pw = canvas_pixwin(xdev->canvas);
  172.     if ( xdev->has_color )
  173.        {    byte Red[64], Green[64], Blue[64];
  174.         int j;
  175.         for ( j = 0; j < 64; j++ )
  176.            {    Red[63 - j] = ((j & 0x30) >> 4) * (255 / 3);
  177.             Green[63 - j] = ((j & 0xc) >> 2) * (255 / 3);
  178.             Blue[63 - j] = ((j & 0x3)) * (255 / 3);
  179.            }
  180.         pw_setcmsname(xdev->pw, "abc");
  181.         pw_putcolormap(xdev->pw, 0, 64, Red, Green, Blue);
  182.        }
  183.     window_set(xdev->frame, WIN_SHOW, TRUE, 0);
  184.     /* Interpose a destroy function to keep Ghostscript from */
  185.     /* getting confused if the user closes the window. */
  186.     notify_interpose_destroy_func(xdev->frame, destroy_func);
  187.     (void) notify_do_dispatch();
  188.     (void) notify_dispatch();
  189.     return 0;
  190. }
  191. /* Prevent the user from closing the window. */
  192. private Notify_value
  193. destroy_func(frame,status)
  194.     Frame frame;
  195.     Destroy_status status;
  196. {    if ( status == DESTROY_CHECKING )
  197.        {    notify_veto_destroy(frame);
  198.         return (NOTIFY_DONE);
  199.        }
  200.     return (notify_next_destroy_func(frame, status));
  201. }
  202.  
  203. /* Close the device. */
  204. int
  205. sun_close(gx_device *dev)
  206. {    window_destroy(xdev->frame);
  207.     xdev->frame = (Frame)0;
  208.     xdev->canvas = (Canvas)0;
  209.     xdev->pw = (Pixwin *)0;
  210.     return 0;
  211. }
  212.  
  213. /* Synchronize the display with the commands already given */
  214. int
  215. sun_sync(register gx_device *dev)
  216. {    (void) notify_dispatch();
  217.     return 0;
  218. }
  219.  
  220. /* Map RGB to color number - simple 64 color system for 8 bit displays */
  221. gx_color_index
  222. sun_map_rgb_color(gx_device *dev, unsigned short red,
  223.     unsigned short green, unsigned short blue)
  224. {    if ( !dev->has_color)
  225.         return gx_default_map_rgb_color(dev, red, green, blue);
  226.     return (63 - ((red << 4) + (green << 2) + blue));
  227. }
  228.  
  229. /* Map color number back to RGB values  - see sun_map_rgb_color(), above */
  230. int
  231. sun_map_color_rgb(gx_device *dev, gx_color_index color,
  232.     unsigned short rgb[3])
  233. {    if ( !dev->has_color )
  234.         return gx_default_map_color_rgb(dev, color, rgb);
  235.     color = 63 - color;
  236.     rgb[0] = (unsigned short)((color & 0x30) >> 4);
  237.     rgb[1] = (unsigned short)((color & 0x0c) >> 2);
  238.     rgb[2] = (unsigned short)((color & 0x03));
  239.     return 0;
  240. }
  241.  
  242. /* Fill a rectangle with a color. */
  243. int
  244. sun_fill_rectangle(register gx_device *dev,
  245.   int x, int y, int w, int h, gx_color_index color)
  246. {    check_rect();
  247.     pw_write(xdev->pw, x, y, w, h, PIX_SRC | PIX_COLOR((int)(color)),
  248.          (Pixrect *)0, 0, 0);
  249.     (void) notify_dispatch();
  250.     return 0;
  251. }
  252.  
  253. /* Copy a monochrome bitmap. */
  254. int
  255. sun_copy_mono(register gx_device *dev,
  256.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  257.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
  258. {
  259.     register int i;
  260.     int nbytes = h * raster;
  261.     extern struct pixrectops mem_ops;
  262. #if !arch_is_big_endian            /* need to swap bits & bytes */
  263. #  define BUF_WIDTH_BYTES (((int)(8.5*DEFAULT_DPI)+15)/16*2)
  264.     byte swap_buf[BUF_WIDTH_BYTES];
  265. #endif
  266.  
  267.     check_rect();
  268.  
  269.     xdev->pr.pr_ops = &mem_ops;
  270.     xdev->pr.pr_width = w + sourcex + 8;
  271.     xdev->pr.pr_height = h;
  272.     xdev->pr.pr_depth = 1;
  273.     xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
  274.     xdev->mpr.md_linebytes = raster;
  275.     xdev->mpr.md_image = (short *)((ulong)base & ~1);
  276. #if !arch_is_big_endian
  277.     /* Reverse the bit order in each byte. */
  278.     for ( i = 0; i < nbytes; i++ ) base[i] = reverse_bits[base[i]];
  279. #endif
  280.     pw_batch_on(xdev->pw);
  281.     if (one != gx_no_color_index)
  282.     {    pw_stencil(xdev->pw, x, y, w, h,
  283.             PIX_SRC | PIX_COLOR(one), &(xdev->pr),
  284.             ((int)base & 1) ? sourcex + 8 : sourcex, 0,
  285.             (Pixrect *)0, 0, 0);
  286.     }
  287.     if (zero != gx_no_color_index)
  288.     {    for (i = 0; i < nbytes; i++) base[i] = ~base[i];
  289.         pw_stencil(xdev->pw, x, y, w, h,
  290.             PIX_SRC | PIX_COLOR(zero), &(xdev->pr),
  291.             ((int)base & 1) ? sourcex + 8 : sourcex, 0,
  292.             (Pixrect *)0, 0, 0);
  293.         for (i = 0; i < nbytes; i++) base[i] = ~base[i];
  294.     }
  295.     pw_batch_off(xdev->pw);
  296. #if !arch_is_big_endian
  297.     /* Reverse the bits back again. */
  298.     for ( i = 0; i < nbytes; i++ ) base[i] = reverse_bits[base[i]];
  299. #endif
  300.     (void) notify_dispatch();
  301.     return 0;
  302. }
  303.  
  304. /* Copy a color bitmap. */
  305. int
  306. sun_copy_color(register gx_device *dev,
  307.   byte *base, int sourcex, int raster, gx_bitmap_id id,
  308.   int x, int y, int w, int h)
  309. {
  310.     register int i;
  311.     extern struct pixrectops mem_ops;
  312.  
  313.     if ( !dev->has_color )
  314.         return sun_copy_mono(dev, base, sourcex, raster, id,
  315.                      x, y, w, h,
  316.                      (gx_color_index)0, (gx_color_index)1);
  317.  
  318.     check_rect();
  319.  
  320.     xdev->pr.pr_ops = &mem_ops;
  321.     xdev->pr.pr_width = w + sourcex + 8;
  322.     xdev->pr.pr_height = h;
  323.     xdev->pr.pr_depth = 8;
  324.     xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
  325.     xdev->mpr.md_linebytes = raster;
  326.     xdev->mpr.md_image = (short *)((ulong)base & ~1);
  327.     pw_write(xdev->pw, x, y, w, h,
  328.          PIX_SRC, &(xdev->pr),
  329.          (((int)base & 1) ? sourcex + 8 : sourcex), 0);
  330.     (void) notify_dispatch();
  331.     return 0;
  332. }
  333.  
  334. /* Draw a line */
  335. int
  336. sun_draw_line(register gx_device *dev,
  337.   int x0, int y0, int x1, int y1, gx_color_index color)
  338. {    pw_vector(xdev->pw, x0, y0, x1, y1, PIX_SRC, color);
  339.     (void) notify_dispatch();
  340.     return 0;
  341. }
  342.